/**
 * 
 */
package com.effectiv.req1.event;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.google.common.eventbus.EventBus;

/**
 * @author Dhrubo
 *
 */
@Component
@Slf4j
public class EventRegistryManager implements ApplicationContextAware{
	
	@Autowired
	private EventBus eventBus;
	
	private ApplicationContext context;
	
	@PostConstruct
	public void register(){
		
		log.info("------------------Enlisting event handlers --------------------");
		
		
		Map<String,EventHandler> eventHandlers = context.getBeansOfType(EventHandler.class);
		
		List<EventHandler> handlers = new ArrayList<EventHandler>(eventHandlers.values());
		for(EventHandler h : handlers){
			eventBus.register(h);
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
		
	}

}
